package com.fx.service.impl;

import cn.hutool.core.date.DateUtil;

import com.fx.entity.AdminTempUser;
import com.fx.entity.SysRole;
import com.fx.mapper.SysRoleMapper;
import com.fx.service.SysRoleService;
import com.fx.util.IdWorker;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Auther: EDZ
 * @Date: 2018-5-24 14:50:
 * @Email: 你的邮箱
 * 概要说明：该类干嘛用的
 **/
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    IdWorker idWorker;
    @Autowired
    SysRoleMapper sysRoleMapper;

    /**
     * 方法名称：获取角色权限
     * 概要说明：根据角色id获取该角色的权限
     *
     * @param roleId
     * @Ather EDZ
     * @Date 2018-5-24 15:00
     * @Param roleId
     * @Return set
     */
    @Override
    public String[] getPerms(String roleId) {
        String perm = sysRoleMapper.getById(roleId).getFlPerms();
        String[] permArr = null;
        if(perm != null){
            permArr = perm.split(",");
        }
        return permArr;
    }

    @Override
    public SysRole getById(String id) {
        return sysRoleMapper.getById(id);
    }

    @Override
    public List<SysRole> listData(Map<String, Object> param) {
        return sysRoleMapper.queryListData(param);
    }

    @Override
    public int getCount(Map<String, Object> param) {
        return sysRoleMapper.queryCount(param);
    }

    @Override
    public int save(SysRole sysRole) {
        sysRole.setFlId(String.valueOf(idWorker.nextId()));
        sysRole.setFlCreatedTime(DateUtil.now());
        sysRole.setFlCreator(((AdminTempUser) SecurityUtils.getSubject().getPrincipal()).getFlName());
        return sysRoleMapper.save(sysRole);
    }

    @Override
    public int updateById(SysRole sysRole) {
        sysRole.setFlUpdatedTime(DateUtil.now());
        return sysRoleMapper.updateById(sysRole);
    }

    @Override
    public int batchDelByIds(String[] ids) {
        return sysRoleMapper.batchRemove(ids);
    }


    @Override
    public List<SysRole> getAllRole() {
        return sysRoleMapper.queryAllRole();
    }
}
